package com.feng.rabbitmq.two;

import com.feng.rabbitmq.utiils.RabbitMQUtils;
import com.rabbitmq.client.Channel;

import java.io.IOException;
import java.util.Scanner;

public class Task01 {
    public static final String QUEUE_NAME = "hello";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();
        /**
         * 第三个参数为true报了如下错误：
         * Exception in thread "main" java.io.IOException
         * 	at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:129)
         * 	at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:125)
         * 	at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:147)
         * 	at com.rabbitmq.client.impl.ChannelN.queueDeclare(ChannelN.java:968)
         * 	at com.rabbitmq.client.impl.recovery.AutorecoveringChannel.queueDeclare(AutorecoveringChannel.java:343)
         * 	at com.feng.rabbitmq.two.Task01.main(Task01.java:14)
         * Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=405, reply-text=RESOURCE_LOCKED - cannot obtain exclusive access to locked queue 'hello' in vhost '/'. It could be originally declared on another connection or the exclusive property value does not match that of the original declaration., class-id=50, method-id=10)
         * 	at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66)
         */
//        channel.queueDeclare(QUEUE_NAME,false,true,false,null);
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        //从控制台获取值
        Scanner in = new Scanner(System.in);
        while (in.hasNext()){
            String message = in.next();
            channel.basicPublish("",QUEUE_NAME,null,message.getBytes());
            System.out.println("发送消息完成："+message);
        }
    }
}
